% 多数据集对比分析工具
% 作者: Chen Qiabang
% 日期: 2025-10-14
% 用途: 对比不同时间段或不同测试的点云数据

function compare_datasets(varargin)
    % 使用示例:
    %   compare_datasets('data1.csv', 'data2.csv')
    %   compare_datasets('data1.csv', 'data2.csv', 'data3.csv')
    %   compare_datasets()  % 交互式选择多个文件
    
    %% 文件选择
    if nargin == 0
        [filenames, pathname] = uigetfile('*.csv', '选择多个CSV文件进行对比', ...
                                         'MultiSelect', 'on');
        if isequal(filenames, 0)
            disp('用户取消了文件选择');
            return;
        end
        
        if ~iscell(filenames)
            filenames = {filenames};
        end
        
        csv_files = cell(length(filenames), 1);
        for i = 1:length(filenames)
            csv_files{i} = fullfile(pathname, filenames{i});
        end
    else
        csv_files = varargin;
    end
    
    num_files = length(csv_files);
    fprintf('准备对比 %d 个数据集\n\n', num_files);
    
    %% 加载所有数据集
    datasets = cell(num_files, 1);
    dataset_names = cell(num_files, 1);
    
    for i = 1:num_files
        fprintf('正在加载数据集 %d: %s\n', i, csv_files{i});
        
        try
            opts = detectImportOptions(csv_files{i});
            opts.DataLine = 6;
            opts.VariableNamesLine = 5;
            data = readtable(csv_files{i}, opts);
            
            % 移除无效数据
            valid_idx = ~isnan(data.Torque_Nm_) & ~isnan(data.Speed_rad_s_);
            
            datasets{i}.torques = data.Torque_Nm_(valid_idx);
            datasets{i}.speeds = data.Speed_rad_s_(valid_idx);
            datasets{i}.leg_ids = data.Leg_ID(valid_idx);
            datasets{i}.motor_ids = data.Motor_ID(valid_idx);
            
            [~, name, ~] = fileparts(csv_files{i});
            dataset_names{i} = name;
            
            fprintf('  ✓ 加载成功: %d 个数据点\n', length(datasets{i}.torques));
        catch ME
            fprintf('  ❌ 加载失败: %s\n', ME.message);
            return;
        end
    end
    
    %% 可视化1: 扭矩分布对比
    fig1 = figure('Name', '扭矩分布对比', 'Position', [50, 50, 1200, 600]);
    
    subplot(1, 2, 1);
    hold on;
    colors = lines(num_files);
    for i = 1:num_files
        histogram(datasets{i}.torques, 50, 'FaceColor', colors(i,:), ...
                 'FaceAlpha', 0.5, 'DisplayName', dataset_names{i});
    end
    xlabel('扭矩 (Nm)', 'FontSize', 11);
    ylabel('频数', 'FontSize', 11);
    title('扭矩分布对比', 'FontSize', 13, 'FontWeight', 'bold');
    legend('Location', 'best');
    grid on;
    hold off;
    
    subplot(1, 2, 2);
    hold on;
    for i = 1:num_files
        histogram(datasets{i}.speeds, 50, 'FaceColor', colors(i,:), ...
                 'FaceAlpha', 0.5, 'DisplayName', dataset_names{i});
    end
    xlabel('速度 (rad/s)', 'FontSize', 11);
    ylabel('频数', 'FontSize', 11);
    title('速度分布对比', 'FontSize', 13, 'FontWeight', 'bold');
    legend('Location', 'best');
    grid on;
    hold off;
    
    %% 可视化2: 点云叠加对比
    fig2 = figure('Name', '点云叠加对比', 'Position', [100, 100, 1000, 700]);
    hold on;
    
    for i = 1:num_files
        scatter(datasets{i}.speeds, datasets{i}.torques, 2, colors(i,:), ...
               'filled', 'MarkerFaceAlpha', 0.3, ...
               'DisplayName', sprintf('%s (n=%d)', dataset_names{i}, ...
                                     length(datasets{i}.torques)));
    end
    
    xlabel('速度 (rad/s)', 'FontSize', 13, 'FontWeight', 'bold');
    ylabel('扭矩 (Nm)', 'FontSize', 13, 'FontWeight', 'bold');
    title('点云数据对比', 'FontSize', 15, 'FontWeight', 'bold');
    legend('Location', 'best', 'FontSize', 10);
    grid on;
    axis tight;
    hold off;
    
    %% 统计对比
    fprintf('\n========================================\n');
    fprintf('数据集统计对比\n');
    fprintf('========================================\n\n');
    
    fprintf('%-25s %10s %12s %12s %12s %12s\n', ...
            '数据集', '数据点', '扭矩均值', '扭矩标准差', '速度均值', '速度标准差');
    fprintf('%s\n', repmat('-', 1, 85));
    
    for i = 1:num_files
        fprintf('%-25s %10d %12.3f %12.3f %12.3f %12.3f\n', ...
                dataset_names{i}, ...
                length(datasets{i}.torques), ...
                mean(datasets{i}.torques), std(datasets{i}.torques), ...
                mean(datasets{i}.speeds), std(datasets{i}.speeds));
    end
    
    fprintf('\n对比分析完成！\n');
end

% 使用示例:
% compare_datasets('test1.csv', 'test2.csv')
% compare_datasets()  % 交互式选择

